/**
 * @Title: FindBooks.java
 * @Package: cn.xxt.book
 * @Description: ��ȡͼ���б?�����뼯����
 * @author: ���� 
 * @version: 1.0  
 * Create at: 2010-8-27 ����02:17:27 
 * Modifycation History:
 * Date      Author      Vversion      Description
 *---------------------------------------------------
 *2010-8-27     TT      1.0            
 */
package cn.xxt.book.dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;
import cn.xxt.tool.ConnectionTool;
import cn.xxt.book.domain.Book;

/**
 * @author TT
 * 
 */
public class FindBooksDao {
	public double allcount() {
		double pall = 0;
		ConnectionTool cfb = new ConnectionTool();
		Connection con = null;
		try {
			con = cfb.getConnection();
			cfb.setStmt(con.createStatement());
			cfb.setRs(cfb.getStmt().executeQuery(
					"select count(bid) as total from book"));
			if (cfb.getRs().next()) {
				pall = cfb.getRs().getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			cfb.close();
		}
		return pall;
	}

	public Set<Book> findbooks(int begin, int offset) {
		Set<Book> findbookSet = new HashSet<Book>();
		ConnectionTool cfb = new ConnectionTool();
		Connection con = null;
		try {
			con = cfb.getConnection();
			cfb
					.setPstmt(con
							.prepareStatement("SELECT book.bid, book.bname, book.isbn, book.author, book.press, book.price, sum(blink.statu) AS kejie, count(blink.bid) AS zongshu FROM blink, book WHERE book.bid = blink.bid GROUP BY book.bid LIMIT ? , ?"));
			cfb.getPstmt().setInt(1, begin);
			cfb.getPstmt().setInt(2, offset);
			cfb.setRs(cfb.getPstmt().executeQuery());
//			System.out.println(cfb.getPstmt().toString());
			while (cfb.getRs().next()) {
				Book book = new Book();
				book.setBid(cfb.getRs().getInt("bid"));
				book.setBname(cfb.getRs().getString("bname"));
			book.setIsbn(cfb.getRs().getString("isbn"));
				book.setAuthor(cfb.getRs().getString("author"));
				book.setPress(cfb.getRs().getString("press"));
				book.setPrice(cfb.getRs().getDouble("price"));
				book.setKejie(cfb.getRs().getInt("kejie"));
				book.setZongshu(cfb.getRs().getInt("zongshu"));
				findbookSet.add(book);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			cfb.close();
		}
		return findbookSet;
	}
	  public String findbooks2(int begin, int offset){
		  FindBooksDao fBooksDao=new FindBooksDao();
		  return fBooksDao.findbooks(begin, offset).toString();
	  }
	public static void main(String[] args) {  
		FindBooksDao fBooksDao=new FindBooksDao();
		System.out.println(fBooksDao.allcount());
		System.out.println(fBooksDao.findbooks(1, 5));
		System.out.println(fBooksDao.findbooks2(1, 5));
	}
	
}
